package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.chinadutyfree.retailer.core.dao.DeliveryMannerDAO;
import com.ecoolex.chinadutyfree.retailer.entity.DeliveryManner;
import com.ecoolex.chinadutyfree.retailer.entity.DeliveryManner.Column;
import com.ecoolex.chinadutyfree.retailer.entity.DeliveryMannerExample;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;


@Service
public class DeliveryMannerManager {

	@Autowired
	private DeliveryMannerDAO dao;

	@Autowired
	private DirectMailGoodsDeliveryMannerManager directMailGoodsDeliveryMannerManager;

	private void verify(DeliveryManner record) {
		BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getName()), ResultCode.VERIFY.build(10), "配送方式名称为空");
	}

	public DeliveryManner add(DeliveryManner record, String createUser) {
		verify(record);

		record.setDeleted(DeliveryManner.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		return record;
	}

	public DeliveryManner update(DeliveryManner record, String updateUser) {
		verify(record);

		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted, Column.createUser, Column.createTime));
		return record;
	}

	public void delete(DeliveryManner record, String updateUser) {
		long directMailGoods
			= directMailGoodsDeliveryMannerManager.countByDeliveryMannerId(record.getDeliveryMannerId());
		BizAssert.isTrue(Check.isLessThan(directMailGoods, 1L),
			ResultCode.BIZ.build(10),
			"有{}个直邮商品使用了该配送方式,无法删除",
			directMailGoods);

		record.setDeleted(DeliveryManner.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted, Column.updateUser, Column.updateTime);
	}

	public DeliveryManner getById(Integer deliveryMannerId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(deliveryMannerId, DeliveryManner.NOT_DELETED);
	}

	public Pagination<DeliveryManner> query(String name, Sorting[] sorts, Page page) {
		DeliveryMannerExample example = new DeliveryMannerExample();
		DeliveryMannerExample.Criteria criteria = example.createCriteria();

		if (Check.notNullOrTrimEmpty(name)) {
			criteria.andNameLike('%' + name
				+ '%');
		}

		criteria.andDeletedEqualTo(DeliveryManner.NOT_DELETED);
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());

		return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
	}
	
	public List<DeliveryManner> queryByUserShoppingCartIds(Integer userId, List<Integer> cartIds) {
		return dao.queryByUserShoppingCartIds(userId, cartIds);
	}
}